package top.xxyu;

import java.io.File;
import java.util.Arrays;

/**
 * HotfixUtil可以将根目录下所有的hotfix合并打包成一个完成的hotfix
 * 打包后的完整zip文件在目录all下
 *
 * @author tobin
 *
 */
public class HotfixUtil {

	public static void doCompressAll(String _srcFolder,String _desFolder){
		String srcFolder = _srcFolder;//压缩包源目录,默认为项目根目录
		String desFolder = _desFolder;//生成压缩包目的目录
		String tmpFolder = "./unzip";//临时解压目录
		String allName ;//生成压缩包文件名，不需要命名，会默认取"最大序号-"+ALL作为文件名
		//清理
		File desiDir = new File(tmpFolder);
		if(desiDir.exists()){
			FileHelper.deleteSubDir(desiDir);
		}else{
			desiDir.mkdirs();
		}
		//文件排序
		File directory = new File(srcFolder);
		File[] tempList = directory.listFiles();
		FileWrapper[] fileWrappers = new FileWrapper[tempList.length];
		for (int i = 0; i < tempList.length; i++) {
			fileWrappers[i] = new FileWrapper(tempList[i]);
		}
		Arrays.sort(fileWrappers);
		File[] sortedFiles = new File[tempList.length];
		for (int i = 0; i < tempList.length; i++) {
			sortedFiles[i] = fileWrappers[i].getFile();
		}
		tempList = sortedFiles;
		allName = sortedFiles[tempList.length-1].getName();
		allName = allName.substring(0,allName.lastIndexOf("."));
		allName = "/" + allName + "-PSAXXX-ALL";
		//解压缩，合并
		for (int i = 0; i < tempList.length; i++) {
			if (tempList[i].isFile()) {
				System.out.println("----------\r\n 文     件：" + tempList[i]);
				File hotfix = tempList[i];
				String fileName = hotfix.getName().toLowerCase();
				if (!fileName.startsWith(DecompressUtil.HOTFIX_PERFIX))
					continue;
				if (!fileName.endsWith(DecompressUtil.HOTFIX_SUFFIX))
					continue;
				// 只处理"hotfix-*.zip"类型的文件
				DecompressUtil.decompress(hotfix.getAbsolutePath(), tmpFolder+allName);
			}
			if (tempList[i].isDirectory()) {
			}
		}
		//压缩
    	Compress compress = new Compress();
        try {
//        	compress.zip(desFolder+allName+".zip", new File(tmpFolder+allName));
        } catch (Exception e) {
            e.printStackTrace();
        }
//        FileHelper.deleteSubDir(desiDir);
	}

	public static void main(String[] args) {
		String srcFolder = ".";//压缩包源目录,默认为项目根目录
		String desFolder = "./all";//生成压缩包目的目录
		HotfixUtil.doCompressAll(srcFolder, desFolder);
	}

}
